package cn.itsource.pethome.pet.controller;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageList;
import cn.itsource.pethome.basic.util.SystemContext;
import cn.itsource.pethome.basic.util.UserContext;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.service.IShopService;
import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.domain.SearchMasterMessage;
import cn.itsource.pethome.pet.query.SearchMasterMessageQuery;
import cn.itsource.pethome.pet.service.ISearchMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description: 描述:
 * @date: 2020/8/23 19:54
 * @author: summer
 * version: 1.0
 */
@RestController
@RequestMapping("/searchMaster")
public class SearchMasterController {

    @Autowired
    private ISearchMasterService searchMasterService;

    @Autowired
    private IShopService shopService;


    /**
     * 功能描述: 发布寻主消息
     * @param
     * @return: cn.itsource.pethome.basic.util.AjaxResult
     * @Date: 2020/8/23
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMessage masterMessage, HttpServletRequest request){
        try {
            masterMessage.setUser(UserContext.getLoginUser(request));
            searchMasterService.publish(masterMessage);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 功能描述: 加载待处理消息
     * @param query
     * @return: cn.itsource.pethome.basic.util.PageList<cn.itsource.pethome.pet.domain.SearchMasterMessage>
     * @Date: 2020/8/25
     */
    @PatchMapping("/loadMessage")
    public PageList<SearchMasterMessage> loadMessage(@RequestBody SearchMasterMessageQuery query){
        Shop shop = shopService.loadByEmployee(SystemContext.getSystemLoginUser());
        //System.out.println("测试"+shop);
        query.setState(0L);
        query.setShopId(shop.getId());
        return searchMasterService.loadAll(query);
    }

    @PostMapping("/handleMessage")
    public AjaxResult handleMessage(@RequestBody Pet pet){
        Shop shop = shopService.loadByEmployee(SystemContext.getSystemLoginUser());
        pet.setShop(shop);
        searchMasterService.handleMessage(pet);
        return new AjaxResult();
    }
}
